﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Zhongchouwwwww.Models;

namespace Zhongchouwwwww.webform
{
    public partial class BrowseProject : System.Web.UI.Page
    {
        private static List<ProjectinfoDTO> processInfos = new List<ProjectinfoDTO>();
        private static string id = "";
        private static string type = "";
        public static string sousuo = "";
        public static string fenye = "";
        public static List<int> ShowIndex_num = new List<int>();

        public static List<ProjectinfoDTO> TempDates = new List<ProjectinfoDTO>();
        public static bool IsPostB = true;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataShow_all.Text = "所有项目(" + BAL.GetDate.GetProjectNums(1) + ")";
                DataShow_ing.Text = "众筹中(" + BAL.GetDate.GetProjectNums(2) + ")";
                DataShow_ed.Text = "已成功(" + BAL.GetDate.GetProjectNums(3) + ")";
                if (IsPostB)
                {
                    processInfos.Clear();
                    processInfos = BAL.GetDate.GetProjectListByType("全部", 1, "", "");
                    DateSet(processInfos, 0);
                    IsPostB = false;
                }
            }
            processInfos = BAL.GetDate.GetProjectListByType("全部", 1, "", "");
            sousuo = Request.QueryString["sousuo"];
            fenye = Request.QueryString["fenye"];
            id = Request.QueryString["id"];
            if (sousuo == null)
            {
                sousuo = "";
            }
            else
            {
                TempDates.Clear();
                TempDates = BAL.GetDate.GetProjectListByType("全部", 1, "", sousuo);
                DateSet(TempDates, 0);
                return;
            }
            if (fenye == null)
            {
                fenye = "";
            }
            else
            {
                if (TempDates.Count == 0)
                {
                    TempDates = processInfos;
                    DateSet(TempDates, int.Parse(fenye));
                }
                else
                {
                    DateSet(TempDates, int.Parse(fenye));
                }
                return;
            }
            if (id == null)
            {
                id = "";
            }
            else
            {
                TempDates.Clear();
                #region 类型获取
                switch (id)
                {
                    case "0":
                        type = "全部";
                        break;
                    case "1":
                        type = "科技";
                        break;
                    case "2":
                        type = "艺术";
                        break;
                    case "3":
                        type = "设计";
                        break;
                    case "4":
                        type = "音乐";
                        break;
                    case "5":
                        type = "影视";
                        break;
                    case "6":
                        type = "出版";
                        break;
                    case "7":
                        type = "动漫";
                        break;
                    case "8":
                        type = "公益";
                        break;
                    case "9":
                        type = "农业";
                        break;
                    case "10":
                        type = "其他";
                        break;
                    case "15":
                        type = "众筹制造";
                        break;
                    case "16":
                        type = "原创手工";
                        break;
                    case "17":
                        type = "硬件设备";
                        break;
                    default:
                        type = "全部";
                        break;
                }
                #endregion
                if (type == "全部")
                {
                    TempDates = processInfos;
                }
                else
                {
                    TempDates = processInfos.Where(ac =>
                    {
                        if (ac.typer == type)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }).ToList();
                }
                if (TempDates.Count==0)
                {
                    TempDates = processInfos;
                }
                DateSet(TempDates, 0);
                
            }
        }



        private void DateSet(List<ProjectinfoDTO> data, int index)
        {
            ShowIndex_num.Clear();
            ShowDate.DataSource = null;
            var (数据源, 分页总数) = BAL.GetDate.GetProjectListByIndex(data);
            for (int i = 0; i < 分页总数; i++)
            {
                ShowIndex_num.Add((i + 1));
            }
            if (index == 0)
            {
                ShowDate.DataSource = data;
            }
            else
            {
                ShowDate.DataSource = 数据源[index - 1];
            }
            ShowDate.DataBind();
        }

        /// <summary>
        /// 排序按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Selects_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (TempDates.Count == 0)
            {
                if (Selects.SelectedValue== "默认排序")
                {
                    TempDates = processInfos;
                    DateSet(TempDates, 0);
                }
                else if (Selects.SelectedValue == "收藏排序")
                {
                    TempDates = processInfos.OrderByDescending(ac => ac.collection).ToList();
                    DateSet(TempDates, 0);
                }else if(Selects.SelectedValue == "进度排序")
                {
                    TempDates = processInfos.OrderByDescending(ac => ac.progress).ToList();
                    DateSet(TempDates, 0);
                }else if (Selects.SelectedValue == "时间排序")
                {
                    TempDates = processInfos.OrderByDescending(ac => ac.CreaData).ToList();
                    DateSet(TempDates, 0);
                }
                
            }
            else
            {
                if (Selects.SelectedValue == "默认排序")
                {
                    DateSet(TempDates, 0);
                }
                else if (Selects.SelectedValue == "收藏排序")
                {
                    TempDates = TempDates.OrderByDescending(ac => ac.collection).ToList();
                    DateSet(TempDates, 0);
                }
                else if (Selects.SelectedValue == "进度排序")
                {
                    TempDates = processInfos.OrderByDescending(ac => ac.progress).ToList();
                    DateSet(TempDates, 0);
                }
                else if (Selects.SelectedValue == "时间排序")
                {
                    TempDates = processInfos.OrderByDescending(ac => ac.CreaData).ToList();
                    DateSet(TempDates, 0);
                }
            }
        }

        /// <summary>
        /// 全部显示按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DataShow_all_Click(object sender, EventArgs e)
        {
            //background - color:orangered;
            DataShow_all.Attributes["style"] = "background-color:orangered;color:white;";
            DataShow_ing.Attributes["style"] = "background-color:white;color:black;";
            DataShow_ed.Attributes["style"] = "background-color:white;color:black;";
            TempDates.Clear();
            processInfos = BAL.GetDate.GetProjectListByType("全部", 1, "", "");
            TempDates = processInfos;
            DateSet(TempDates, 0);

        }
        /// <summary>
        /// 众筹中按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DataShow_ing_Click(object sender, EventArgs e)
        {
            DataShow_all.Attributes["style"] = "background-color:white;color:black;";
            DataShow_ing.Attributes["style"] = "background-color:orangered;color:white;";
            DataShow_ed.Attributes["style"] = "background-color:white;color:black;";
            TempDates.Clear();
            processInfos = BAL.GetDate.GetProjectListByType("全部", 1, "", "");
            TempDates = processInfos.Where(ac=> {
                if (ac.state=="众筹中")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }).ToList();
            DateSet(TempDates, 0);
        }
        /// <summary>
        /// 已成功按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DataShow_ed_Click(object sender, EventArgs e)
        {
            DataShow_all.Attributes["style"] = "background-color:white;color:black;";
            DataShow_ing.Attributes["style"] = "background-color:white;color:black;";
            DataShow_ed.Attributes["style"] = "background-color:orangered;color:white;";
            TempDates.Clear();
            processInfos = BAL.GetDate.GetProjectListByType("全部", 1, "", "");
            TempDates = processInfos.Where(ac => {
                if (ac.state == "已完成")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }).ToList();
            DateSet(TempDates, 0);
        }
    }
}